package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleHomeMapper;
import com.heima.article.service.ArticleHomeService;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2023/1/9 17:05
 */
@Service
@Slf4j
public class ArticleHomeServiceImpl extends ServiceImpl<ArticleHomeMapper, ApArticle> implements ArticleHomeService {


    /**
     * @description 页面展示
     * @param articleHomeDto
     * @param loadType
     * @return com.heima.model.dtos.ResponseResult
     * @author 31115
     * @date 2023/1/9 17:18
     */
    @Override
    public ResponseResult load(ArticleHomeDto articleHomeDto, String loadType) {

        if (loadType==null){
            loadType = "load";
        }

        if ("load".equals(loadType)){
            //页面展示
            List<ApArticle> load = loadArticleList(articleHomeDto);
            return ResponseResult.okResult(load);
        } else if ("loadmore".equals(loadType)) {
            //下拉更多
            List<ApArticle> loadmore = loadArticleList(articleHomeDto);
            return ResponseResult.okResult(loadmore);
        }else if("loadnew".equals(loadType)){
            //上拉最近数据
            List<ApArticle> loadnew = loadArticleList(articleHomeDto);
            return ResponseResult.okResult(loadnew);
        }
        log.info("页面详情，请求不存在检查uri");
        return null;
    }

    private List<ApArticle> loadArticleList(ArticleHomeDto dto){

        if (StringUtils.isEmpty(dto.getSize()) || dto.getSize() == 0){
            log.debug("未传分页size：{}",dto.getSize());
            dto.setSize(10);
        }
        if (StringUtils.isEmpty(dto.getMaxBehotTime())){
            dto.setMaxBehotTime(new Date());
        }
        if (StringUtils.isEmpty(dto.getMinBehotTime())){
            dto.setMinBehotTime(new Date());
        }
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        List<ApArticle> loadArticleList = this.getBaseMapper().loadArticleList(dto);
        return loadArticleList;
    }

}
